<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require ('./configs/site_config.php');
require('./classes/control/control_comment.php');
require('./classes/control/control_product.php');

class Comment
{
	var $comment_c;
	var $product_c;

	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	
	public function __construct()
	{
	
		$this->comment_c = new CommentControl();
		$this->product_c = new ControlProduct();

		
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{


		if($_GET['in_jax'] == 1 )//修改
		{
			$value = '';
			if (!empty($_GET['value'])) {
				
				$value = $_GET['value'];
			}
			//修改显示图片
			if ($_GET['type'] == 'show') {
				
				$field = 'is_show';
				
				//修改模板修改时间
				
			 	$this->product_c->updateTemplate($_GET['product_id'], '', 1 );
			 
				
			} elseif($_GET['type'] == 'sort') {//修改排序
				$field = 'sort';
				$value = (int)$_GET['value'];
			}
			
			if (!empty($_GET['comment_id'])) {
				
				echo $this->comment_c->change_info((int) $_GET['comment_id'],$value,$field,'show');
				exit;
			}
		
		}
		
		if(!empty($_REQUEST['product_id']))
		{
			$product_id = (int)$_REQUEST['product_id'];
			$url_info = $comment_info = array();
			//分页处理
			if($product_id) {
				$product_name = $this->comment_c->get_product_name($product_id);
			}
			$page = 1;
			if (!empty($_REQUEST['page'])) 
			{
				$page = (int)$_REQUEST['page'];
			}
			$url_info = $this->comment_c->get_url_info($page,'list',$product_id);
			$comment_info = $this->comment_c->get_comment_info('list',$url_info['page_info']['start'],20,$product_id);
			$GLOBALS['smarty']->assign('product_id',   $product_id);
			$GLOBALS['smarty']->assign('product_name',   $product_name);
			$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
			$GLOBALS['smarty']->assign('comment_info',$comment_info );
			$GLOBALS['smarty']->assign('search',$comment_type);
			$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
			$GLOBALS['smarty']->display('comment_index.htm'); 
		}

	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function add_edit()
	{	
		if($_REQUEST['act'] == 'add')
		{
			if(!empty($_REQUEST['product_id']))
			{
				$product_name = $this->comment_c->get_product_name($_REQUEST['product_id']);
				if($_POST['sub_from'])
				{
					if(!empty($_POST['product_id']))
					{
						$affect = $this->comment_c->operate_comment('add',0,$_POST['user_name'],$_POST['country'],
						$_POST['sort'],$_POST['is_show'],$_POST['content'],(int)$_POST['product_id'],trim($_POST['product_name']));
						if($affect == 1)
						{
						    $product_id = (int)$_POST['product_id'];
						    echo  "<script>alert('增加成功！');
						    window.location.href='/wondershare/comment.php?action=index&product_id=$product_id'</script>";
						    exit;
						}
					}
				}
				$GLOBALS['smarty']->assign('tile',     '添加');
				$GLOBALS['smarty']->assign('form_act',     'add');
				$GLOBALS['smarty']->assign('product_id',     (int)$_GET['product_id']);
				$GLOBALS['smarty']->assign('product_name',     $product_name);
				$GLOBALS['smarty']->display('comment_info.htm');
			}else
			{
			
				echo  '<script type="text/javascript"> alert("请选择产品再添加留言！");history.go(-1);</script>';
				exit;
			}
		
		}
		elseif($_REQUEST['act'] == 'edit')
		{
			$comment_info = array();
			if($_POST['sub_from'])
			{
				if(!empty($_POST['comment_id']))
				{
					 $affect = $this->comment_c->operate_comment('edit',(int)$_POST['comment_id'],$_POST['user_name'],$_POST['country'],
					 $_POST['sort'],$_POST['is_show'],$_POST['content']);
					 if($affect == 1)
					 {
					    echo  '<script type="text/javascript"> alert("编辑成功！"); </script>';
					    
					    //修改模板修改时间
					 	$location = '/wondershare/comment.php?action=index&product_id='.$_POST['product_id'];
					 	$this->product_c->updateTemplate($_POST['product_id'], $location );
					 }
				}
			}
			$GLOBALS['smarty']->assign('product_id',     (int)$_GET['product_id']);
			$comment_info = $this->comment_c->get_comment_id_info((int)$_GET['comment_id']);
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('comment_info',$comment_info[0] );
			$GLOBALS['smarty']->display('comment_info.htm'); 	
		}
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->comment_c->delete_comment($checkList);
		}
	}
}

$action    = $_REQUEST['action']; 
$Comment   = new Comment;
if(method_exists($Comment, $action)) 
{
$Comment->$action();
}else{
$Comment->index();
}

?>
